/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 08 - 25
 * Time: 11:36
 */
//牛客 删除有序链表中重复的元素-II
class ListNode {
    int val;
   ListNode next = null;
    public ListNode(int val) {
      this.val = val;
    }
  }
public class Solution1 {
    //法一 我的方法
    public ListNode deleteDuplicates (ListNode head) {
        if(head == null) return null;
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;
        ListNode pre = dummyNode;
        ListNode cur = head;
        while(cur != null && cur.next != null) {
            if(cur.val == cur.next.val) {
                int x = cur.val;
                while(cur != null && cur.val == x) {
                    cur = cur.next;
                }
                pre.next = cur;
            }else {
                pre = cur;
                cur = cur.next;
            }
        }
        return dummyNode.next;
    }

    //法二 官方方法
    public ListNode deleteDuplicates1 (ListNode head) {
        if(head == null) return null;
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;
        ListNode cur = dummyNode;
        while(cur.next != null && cur.next.next != null) {
            if(cur.next.val == cur.next.next.val) {
                int temp = cur.next.val;
                while(cur.next != null && cur.next.val == temp) {
                    cur.next = cur.next.next;
                }
            }else {
                cur = cur.next;
            }
        }
        return dummyNode.next;
    }
}
